package com.leorui.schoollife.mapper

import com.leorui.schoollife.entity.NoteEntity
import com.leorui.schoollife.entity.SchoolEntity
import com.leorui.schoollife.entity.StudentAccount
import org.apache.ibatis.annotations.*
import org.springframework.web.bind.annotation.RequestParam

@Mapper
interface IDBUser {
    // 查询账号是否存在
    @Select("select count(account) from user_student where account = #{account}")
    fun queryAccountIsSave(@Param("account") account: String): Int?

    // 查询账号和密码
    @Select("select count(*) from user_student where account = #{account} and password = #{password}")
    fun checkAccountAndPassword(@Param("account") account: String, @Param("password") password: String): Int?

    // 添加普通账号
    @Insert("insert into user_student(account, password) value(#{account}, #{password})")
    fun addStudentAccount(@Param("account") account: String, @Param("password") password: String)

    // 删除账号
    @Delete("delete from user_student where account = #{account}")
    fun deleteStudentAccount(account: String)

    // 查询账号权限
    @Select("select power from user_student where account = #{account}")
    fun selectAccountPower(@Param("account") account: String): Int?

    // 更改用户密码
    @Update("update user_student set password = #{password} where account = account")
    fun changeUserPassword(@Param("account") account: String, @Param("password") password: String): Int?

    // 获取用户的账号部分信息
    @Select("select account, nickname, icon_id, icon_src " +
            "from user_student left join user_icon on user_student.icon = user_icon.icon_id " +
            "where account = #{account}")
    @Results(value = [
        Result(id = true, column = "account", property = "account"),
        Result(column = "nickname", property = "nickname"),
        Result(column = "icon_src", property = "icon.iconSrc"),
        Result(column = "icon_id", property = "icon.iconId"),
    ])
    fun getUserAccount(@Param("account") account: String): StudentAccount

    // 查询用户对应的学校
    @Select("select user_school.school_id, user_school.school_name, school_web_url, parse_type from user_school, user_student where user_student.school_id = user_school.school_id and user_student.account = #{account}")
    @Results(value = [
        Result(id = true, column = "school_id", property = "schoolId"),
        Result(column = "school_name", property = "schoolName"),
        Result(column = "school_web_url", property = "schoolWebUrl"),
        Result(column = "parse_type", property = "parseType"),
    ])
    fun selectUserSchool(@Param(value = "account") account: String): SchoolEntity?

    // 查询所有的学校信息
    @Select("select school_id, school_name, school_web_url, parse_type from user_school")
    @Results(value = [
        Result(id = true, column = "school_id", property = "schoolId"),
        Result(column = "school_name", property = "schoolName"),
        Result(column = "school_web_url", property = "schoolWebUrl"),
        Result(column = "parse_type", property = "parseType"),
    ])
    fun selectAllSchool(): List<SchoolEntity?>?

    // 更改用户学校
    @Update("update user_student set school_id = #{schoolId} where account = #{account}")
    fun changeUserSchool(@Param(value = "schoolId") schoolId: Int, @Param(value = "account") account: String): Int?

    // --------------------- 用户备忘录信息------------------
    // 查询用户备忘录信息
    @Select("select note_id, note_msg, title, record_start_time, last_edit_time, account from user_memorandum " +
            "where account = #{account} order by last_edit_time desc limit #{page_size} offset #{offset}")
    @Results(value = [
        Result(id = true, column = "note_id", property = "noteId"),
        Result(id = true, column = "account", property = "account"),
        Result(column = "title", property = "title"),
        Result(column = "note_msg", property = "msg"),
        Result(column = "record_start_time", property = "recordStartTime"),
        Result(column = "last_edit_time", property = "lastEditTime"),
    ])
    fun pullUserMemorandumList(
            @Param(value = "account") account: String,
            @Param(value = "page_size") pageSize: Int?,
            @Param(value = "offset") offset: Int?
    ): List<NoteEntity?>?

    // 修改用户备忘录
    @Update("update user_memorandum set title = #{title}, note_msg = #{msg}, last_edit_time = #{last_edit_time} " +
            "where account = #{account} and note_id = #{note_id}")
    fun updateUserMemorandum(
            @Param(value = "account") account: String,
            @Param(value = "note_id") note_id: String,
            @Param(value = "title") title: String,
            @Param(value = "msg") msg: String,
            @Param(value = "last_edit_time") last_edit_time: String
    ): Int?

    // 删除用户备忘录
    @Delete("delete from user_memorandum where account = #{account} and note_id = #{note_id}")
    fun deleteUserMemorandum(
            @Param(value = "account") account: String,
            @Param(value = "note_id") note_id: String
    ): Int?

    // 添加用户备忘录
    @Insert("insert into user_memorandum(note_id, note_msg, title, record_start_time, last_edit_time,account) " +
            "value(#{note_id}, #{msg}, #{title}, #{record_start_time}, #{last_edit_time}, #{account})")
    fun addUserMemorandum(
            @Param(value = "account") account: String,
            @Param(value = "note_id") note_id: String,
            @Param(value = "title") title: String,
            @Param(value = "msg") msg: String,
            @Param(value = "record_start_time") record_start_time: String,
            @Param(value = "last_edit_time") last_edit_time: String
    ): Int?

    // 查询所有备忘录条目
    @Select("select count(note_id) from user_memorandum where account = #{account}")
    fun countUserAllMemorandum(@Param(value = "account") account: String): Int?

    // 查询用户单条备忘录
    @Select("select note_id, note_msg, title, record_start_time, last_edit_time, account " +
            "from user_memorandum  where account = #{account} and note_id = #{noteId}")
    @Results(value = [
        Result(id = true, column = "note_id", property = "noteId"),
        Result(id = true, column = "account", property = "account"),
        Result(column = "title", property = "title"),
        Result(column = "note_msg", property = "msg"),
        Result(column = "record_start_time", property = "recordStartTime"),
        Result(column = "last_edit_time", property = "lastEditTime"),
    ])
    fun selectUserMemorandum(@Param(value = "account") account: String, @Param(value = "noteId") noteId: String): NoteEntity?
}